#include "PlainPolyRenderer.h"
#include "SDL_opengl.h"
#include <iostream>

namespace k2graphics
{

PlainPolyRenderer::PlainPolyRenderer()
{
	setColor(0, 0.0f, 1.0f, 1.0f);
	setColor(1, 1.0f, 0.0f, 0.0f);
	setColor(2, 0.0f, 1.0f, 0.0f);
	setColor(3, 0.0f, 0.0f, 1.0f);
}

void PlainPolyRenderer::renderObject(RenderData* data, GraphicsInternals* gi)
{
	// the line should have at least 9 float values and
	// one byte value (color)

	if(data->numBytes < 1 || data->numFloats < 9)
		return;

	//std::cout << "PlainPoly\n";

	int theColor = data->byteData[0];

	float cx = data->floatData[0];
	float cy = data->floatData[1];
	float ctheta = (data->floatData[2] / PI) * 180.0;

	int numSides = (data->numFloats - 3) / 2;

	// set openGL color
	glColor3f(presetColors[theColor][0], presetColors[theColor][1], presetColors[theColor][2]);

	// set up transforms
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glTranslatef(cx, cy, 0.0f);
	glRotatef(ctheta, 0.0f, 0.0f, 1.0f);

	// draw the polygon
	glBegin(GL_LINE_LOOP);

	for(int i = 3; i < (numSides * 2) + 3; i += 2)
	{
		glVertex2f(data->floatData[i + 0], data->floatData[i + 1]);
	}

	glEnd();

	// undo transforms
	glPopMatrix();
}

void PlainPolyRenderer::setColor(int color, float r, float g, float b)
{
	presetColors[color][0] = r;
	presetColors[color][1] = g;
	presetColors[color][2] = b;
}

}
